package com.wangyadong.hobby.schedule.jobtask.ath.lesson1.stackqueue;

import com.wangyadong.hobby.schedule.jobtask.ath.common.StdIn;
import com.wangyadong.hobby.schedule.jobtask.ath.common.StdOut;
import com.wangyadong.hobby.schedule.jobtask.ath.lesson1.Stack;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/4/1713:31
 **/
public class Evaluate1 {
//( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
    //(1+(2+3)*(4*5))
    public static void main(String[] args) {
        Stack<String> ops = new Stack<>();
        Stack<Double> vals = new Stack<>();

        while (!StdIn.isEmpty()) {
            String s = StdIn.readString();
            if (s.equals("(")) {

            } else if (s.equals("+")) {
                ops.push(s);
            } else if (s.equals("-")) {
                ops.push(s);
            } else if (s.equals("*")) {
                ops.push(s);
            } else if (s.equals("/")) {
                ops.push(s);
            } else if (s.equals("sqrt")) {
                ops.push(s);
            } else if (s.equals(")")) {
                String pop = ops.pop();
                Double value = vals.pop();
                if (pop.equals("+")) {
                    value = value + vals.pop();
                } else if (pop.equals("-")) {
                    value = value - vals.pop();
                } else if (pop.equals("*")) {
                    value = value * vals.pop();
                } else if (pop.equals("/")) {
                    value = value / vals.pop();
                }
                vals.push(value);

            } else {
                vals.push(Double.parseDouble(s));
            }
        }
        StdOut.println("--->" + vals.pop());
    }
}
